﻿
#include <QDebug>
#include <QRegExp>
#include <iostream>
#include <string>

#include "appsence.h"
#include "stock.h"
#include "broker.h"
#include "movestock.h"

using namespace std;

int main()
{
    AppSence *sence = new AppSence();
    Broker *broker = new Broker();
    Stock *aStock = new Stock("a", sence);
    Stock *bStock = new Stock("b", sence);


    qDebug()<<"All Stock are:"<<sence->allStockID();

    qDebug()<<"Current Stock is:"<<sence->currentStock()->id();
    qDebug()<<"Valid cmd like this:\n"
              "1. move (40, 80)\n"
              "2. quit\n"
              "3. redo\n"
              "4. undo";
    QString qstrCmd;
    while (qstrCmd != "quit") {
        cout<<"Please enter cmd:";
        string strcmd;
        getline(cin, strcmd);
        QString qStrCmd = QString::fromStdString(strcmd);
        if(qStrCmd == QString("quit")){
            delete sence;
            delete broker;
            delete aStock;
            delete bStock;
            exit(0);
        }
        if(qStrCmd.left(4) == QString("move")){
            QString strPos = qStrCmd.remove("move").remove(" ").remove("(").remove(")");
            int x = strPos.split(',').first().toInt();
            int y = strPos.split(',').last().toInt();
            QPoint p = QPoint(x, y);
            QPair<QPoint, QPoint> path(sence->currentStock()->pos(), p);
            MoveStock *moveCmd = new MoveStock(sence->currentStock(), path);
            broker->takeOrder(moveCmd);
            qDebug()<<"----------result------"<<sence->currentStock()->pos();
        }
        if(qStrCmd == QString("redo")){
            broker->redoOrder();
            qDebug()<<"----------result------"<<sence->currentStock()->pos();
        }
        if(qStrCmd == QString("undo")){
            broker->undoOrder();
            qDebug()<<"----------result------"<<sence->currentStock()->pos();
        }
    }
    return 0;
}
